package logic

import (
	"context"
	"errors"
	"taobao-merchandise/models/mysql"

	"taobao-merchandise/taobaorpc/internal/svc"
	"taobao-merchandise/taobaorpc/taobaorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsUpdateLogic {
	return &GoodsUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsUpdateLogic) GoodsUpdate(in *taobaorpc.GoodsUpdateRequest) (*taobaorpc.GoodsUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var goods mysql.Goods

	if err := goods.FindGoodsById(mysql.DB, in.GoodsId); err != nil {
		return nil, errors.New("商品不存在")
	}
	goods.Title = in.Title
	goods.Price = in.Price
	goods.Image = in.Image

	if err := goods.GoodsUpdate(mysql.DB, goods.ID); err != nil {
		return nil, errors.New("修改失败")
	}

	return &taobaorpc.GoodsUpdateResponse{
		GoodsId: int64(goods.ID),
	}, nil
}
